package com.wlyuan.index.domain.source.valueobj;

import com.wlyuan.core.domain.Identifier;
import com.wlyuan.core.domain.valueobj.*;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.var;

@Getter
@EqualsAndHashCode(callSuper = true)
public final class SourceId extends Identifier<String> {
    private final Modifiers modifier;

    public SourceId(OrderId orderId) {
        this(Modifiers.ORDER, orderId);
    }

    public SourceId(WaybillId waybillId) {
        this(Modifiers.WAYBILL, waybillId);
    }

    public SourceId(CombineId combineId) {
        this(Modifiers.COMBINE, combineId);
    }

    public SourceId(QuotationId quotationId) {
        this(Modifiers.QUOTATION, quotationId);
    }

    public SourceId(BillId billId) {
        this(Modifiers.BILL, billId);
    }

    public <T extends Identifier<?>> SourceId(Modifiers modifier, T dataId) {
        super(modifier.getValue() + ":" + dataId.getId());
        this.modifier = modifier;
    }

    public <T> T decode() {
        var splices = this.getId().split(":");
        if (Modifiers.WAYBILL == this.modifier) {
            return (T) new WaybillId(Long.valueOf(splices[1]));
        } else if (Modifiers.COMBINE == this.modifier) {
            return (T) new CombineId(Long.valueOf(splices[1]));
        } else if (Modifiers.QUOTATION == this.modifier) {
            return (T) new QuotationId(Long.valueOf(splices[1]));
        } else if (Modifiers.BILL == this.modifier) {
            return (T) new BillId(Long.valueOf(splices[1]));
        } else if (Modifiers.PAYMENT == this.modifier) {
            return (T) new WaybillId(Long.valueOf(splices[1]));
        } else if (Modifiers.ORDER == this.modifier) {
            return (T) new OrderId(Long.valueOf(splices[1]));
        }
        return null;
    }

    @Override
    public String toString() {
        return this.getId();
    }
}